<!DOCTYPE html>
<html>
<head>
    <title>垃圾分类系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .header {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 20px 30px;
            margin-bottom: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
        }
        
        .logo {
            display: flex;
            align-items: center;
            gap: 12px;
        }
        
        .logo h1 {
            color: #2d3748;
            font-size: 24px;
            font-weight: 700;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 12px;
            flex-wrap: wrap;
        }
        
        .user-welcome {
            color: #4a5568;
            font-weight: 500;
        }
        
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            text-decoration: none;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }
        
        .btn-knowledge {
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            color: white;
        }
        
        .btn-knowledge:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
        }
        
        .btn-admin {
            background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);
            color: white;
        }
        
        .btn-admin:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
        }
        
        .btn-profile {
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
            color: white;
        }
        
        .btn-profile:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
        }
        
        .btn-shop {
            background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
            color: white;
        }
        
        .btn-shop:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
        }
        
        .btn-stats {
            background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
            color: white;
        }
        
        .btn-stats:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(6, 182, 212, 0.3);
        }
        
        .btn-forum {
            background: linear-gradient(135deg, #ec4899 0%, #db2777 100%);
            color: white;
        }
        
        .btn-forum:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(236, 72, 153, 0.3);
        }
        
        .btn-logout {
            background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
            color: white;
        }
        
        .btn-logout:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
        }
        
        .main-content {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            margin-bottom: 30px;
        }
        
        .card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            animation: slideUp 0.6s ease-out;
        }
        
        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .upload-card {
            text-align: center;
        }
        
        .upload-card h2 {
            color: #2d3748;
            font-size: 24px;
            margin-bottom: 12px;
        }
        
        .upload-card p {
            color: #718096;
            margin-bottom: 30px;
        }
        
        .upload-area {
            border: 2px dashed #cbd5e0;
            border-radius: 16px;
            padding: 40px 20px;
            margin-bottom: 20px;
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .upload-area:hover {
            border-color: #667eea;
            background: rgba(102, 126, 234, 0.05);
        }
        
        .upload-area.dragover {
            border-color: #667eea;
            background: rgba(102, 126, 234, 0.1);
        }
        
        .upload-icon {
            font-size: 48px;
            margin-bottom: 16px;
            color: #a0aec0;
        }
        
        .file-input {
            display: none;
        }
        
        .file-input-button {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 12px;
        }
        
        .file-input-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
        }
        
        .file-name {
            color: #718096;
            font-size: 14px;
            margin-bottom: 20px;
        }
        
        .preview-container {
            margin: 20px 0;
            display: none;
        }
        
        .image-preview {
            max-width: 100%;
            max-height: 300px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
        
        .submit-button {
            background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
            color: white;
            padding: 14px 28px;
            border: none;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            width: 100%;
        }
        
        .submit-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(72, 187, 120, 0.3);
        }
        
        .submit-button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }
        
        .result-card {
            text-align: center;
        }
        
        .result-card h2 {
            color: #2d3748;
            font-size: 24px;
            margin-bottom: 20px;
        }
        
        .result-image {
            max-width: 100%;
            max-height: 300px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
        
        .prediction-result {
            background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
            color: white;
            padding: 16px 24px;
            border-radius: 12px;
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 20px;
        }
        
        .probabilities {
            background: #f7fafc;
            border-radius: 12px;
            padding: 20px;
            text-align: left;
        }
        
        .probabilities h4 {
            color: #2d3748;
            margin-bottom: 12px;
            font-size: 16px;
        }
        
        .prob-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 0;
            border-bottom: 1px solid #e2e8f0;
        }
        
        .prob-item:last-child {
            border-bottom: none;
        }
        
        .prob-label {
            color: #4a5568;
            font-weight: 500;
        }
        
        .prob-value {
            color: #2d3748;
            font-weight: 600;
        }
        
        .error-message {
            background: #fed7d7;
            color: #c53030;
            padding: 16px;
            border-radius: 12px;
            border-left: 4px solid #e53e3e;
            margin-top: 20px;
        }
        
        .api-info {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .api-info h3 {
            color: #2d3748;
            font-size: 20px;
            margin-bottom: 16px;
        }
        
        .api-endpoint {
            background: #f7fafc;
            border-radius: 8px;
            padding: 12px 16px;
            margin-bottom: 12px;
            border-left: 4px solid #667eea;
        }
        
        .api-endpoint strong {
            color: #2d3748;
        }
        
        .api-endpoint span {
            color: #718096;
        }
        
        @media (max-width: 768px) {
            .main-content {
                grid-template-columns: 1fr;
            }
            
            .header {
                flex-direction: column;
                gap: 16px;
                text-align: center;
            }
            
            .user-info {
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header class="header">
            <div class="logo">
                <span style="font-size: 28px;">🗂️</span>
                <h1>垃圾分类系统</h1>
            </div>
            <div class="user-info">
                <span class="user-welcome">欢迎，{{ session['username'] }}</span>
                <a href="/knowledge" class="btn btn-knowledge">📚 科普知识</a>
                <a href="/profile" class="btn btn-profile">👤 个人中心</a>
                <a href="/points_shop" class="btn btn-shop">🛒 积分商城</a>
                <a href="/statistics" class="btn btn-stats">📈 数据统计</a>
                <a href="/forum" class="btn btn-forum">💬 环保社区</a>
                {% if is_admin %}
                <a href="/admin" class="btn btn-admin">📊 管理后台</a>
                {% endif %}
                <a href="/logout" class="btn btn-logout">🚪 注销</a>
            </div>
        </header>
        
        <div class="main-content">
            <div class="card upload-card">
                <h2>🔍 智能识别</h2>
                <p>上传垃圾图片，AI将为您智能分类</p>
                
                <form action="/" method="post" enctype="multipart/form-data">
                    <div class="upload-area" onclick="document.getElementById('file').click()">
                        <div class="upload-icon">📷</div>
                        <input type="file" name="file" id="file" accept=".jpg,.jpeg,.png" class="file-input" onchange="previewImage()">
                        <button type="button" class="file-input-button" onclick="document.getElementById('file').click()">选择图片</button>
                        <div id="file-name" class="file-name">支持 JPG、PNG 格式</div>
                    </div>
                    
                    <div id="preview-container" class="preview-container">
                        <img id="preview-image" class="image-preview" src="" alt="预览">
                    </div>
                    
                    <button type="submit" class="submit-button" id="submit-btn" disabled>🚀 开始识别</button>
                </form>
            </div>
            
            <div class="card result-card" style="display: {{ 'block' if prediction_result or image_data or error_message else 'none' }};">
                <h2>📊 识别结果</h2>
                
                {% if image_data %}
                <img src="data:image/jpeg;base64,{{ image_data }}" class="result-image">
                {% endif %}
                
                {% if category_display %}
                <div class="prediction-result">
                    🎯 {{ category_display|safe }}
                </div>
                {% endif %}
                
                {% if probabilities_display %}
                <div class="probabilities">
                    <h4>📈 置信度分析</h4>
                    {{ probabilities_display|safe }}
                </div>
                {% endif %}
                
                {% if error_message %}
                <div class="error-message">
                    ❌ 识别失败: {{ error_message }}
                </div>
                {% endif %}
            </div>
        </div>
        
        <div class="api-info">
            <h3>🔌 API 接口</h3>
            <div class="api-endpoint">
                <strong>POST /predict</strong> - <span>上传图片获取分类预测</span>
            </div>
            <div class="api-endpoint">
                <strong>GET /health</strong> - <span>检查API健康状态</span>
            </div>
        </div>
    </div>
    
    <script>
        function previewImage() {
            const fileInput = document.getElementById('file');
            const fileNameSpan = document.getElementById('file-name');
            const previewContainer = document.getElementById('preview-container');
            const previewImage = document.getElementById('preview-image');
            const submitBtn = document.getElementById('submit-btn');
            
            if (fileInput.files.length > 0) {
                const file = fileInput.files[0];
                fileNameSpan.textContent = file.name;
                submitBtn.disabled = false;
                
                // 显示图片预览
                const reader = new FileReader();
                reader.onload = function(e) {
                    previewImage.src = e.target.result;
                    previewContainer.style.display = 'block';
                };
                reader.readAsDataURL(file);
            } else {
                fileNameSpan.textContent = '支持 JPG、PNG 格式';
                previewContainer.style.display = 'none';
                submitBtn.disabled = true;
            }
        }
        
        // 拖拽上传功能
        const uploadArea = document.querySelector('.upload-area');
        
        uploadArea.addEventListener('dragover', function(e) {
            e.preventDefault();
            uploadArea.classList.add('dragover');
        });
        
        uploadArea.addEventListener('dragleave', function(e) {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
        });
        
        uploadArea.addEventListener('drop', function(e) {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                const fileInput = document.getElementById('file');
                fileInput.files = files;
                previewImage();
            }
        });
    </script>
</body>
</html>